package com.share.two.bio.文件上传;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

import com.sun.org.apache.xml.internal.security.keys.content.RetrievalMethod;

@SuppressWarnings("resource")
public class Client {

	public static void main(String[] args) throws Exception {
		File file = getFlie();
		
		Socket socket = new Socket("127.0.0.1", 54321);
		BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
		PrintStream ps = new PrintStream(socket.getOutputStream());
		
		ps.println(file.getName());
		
		String result = br.readLine();
		if ("存在".equals(result)) {
			socket.close();
			return;
		}
		
		FileInputStream fi = new FileInputStream(file);
		byte[] arr = new byte[8*1024];
		int len;
		while ((len = fi.read(arr)) != -1) {
			ps.write(arr, 0, len);
		}
		
		fi.close();
		socket.close();
		
	}

	private static File getFlie() {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入文件名。。。");
		
		while (true) {
			String dirAndFileName = scanner.nextLine();
			File file = new File(dirAndFileName);
			if (!file.exists()) {
				System.out.println("文件不存在。。。");
			}else if (file.isDirectory()) {
				System.out.println("is文件夹， not文件。。。");
			}else {
				return file;
			}
		}
		
	}
}
